package com.jdkj.tools.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jdkj.tools.domain.TestCase;
import lombok.SneakyThrows;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UpLoadUtil {

    @SneakyThrows
    public static void UpLoadFile(String projectKey, String folderId, List<TestCase> cases, String projectId) {

        Map<String, String> map = new HashMap<>();
        //获得表单

        Header[] headers = new Header[]{new BasicHeader("Cookie", TokenConstant.JSESSIONID),
//                new BasicHeader("referer", "http://192.168.11.7:8083/secure/ViewProfile.jspa"),
                new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"),
//                new BasicHeader("x-ausername", "zhouxin"),
        };
        /*
        TestCase(feature=, softwareRequirement=, caseId=SWR-CON-01-01, testPurpose=1、远程控制登录,
         preCondition=1、使用SSCOM V5.13工具,
        testSteps=1、左下角选择TCPClient，远程地址为：139.196.46.31:8888，点击连接, expectedResult=1、登录成功,
        testResult=PASS, testData=, comment=)
         */
        String postUrl = "http://192.168.11.7:8083/secure/QuickCreateIssue.jspa?decorator=none";
        for (int i = 0; i <cases.size(); i++) {
            map.putAll(getFormData());
            TestCase tc = cases.get(i);
            System.out.println(tc);
            map.put("pid", projectId);
            map.put("issuetype", "10300");
            map.put("summary", tc.getCaseId());
//            map.put("summary", UUID.randomUUID().toString().substring(0,10));
            map.put("assignee", "wangduoduo");
            map.put("reporter", "wangduoduo");
            map.put("customfield_10401", tc.getTestPurpose());
            map.put("customfield_10402", tc.getPreCondition());
            map.put("customfield_10400", tc.getTestSteps());
            map.put("customfield_10404", tc.getExpectedResult());
            String result = HttpClientUtil.doPost(postUrl, map, headers);
            System.out.println("上传表单的"+result);
            String issueKey=((JSONObject)JSON.parse(result)).getString("issueKey");
            System.out.println("验证token"+TokenConstant.JSESSIONID);
            System.out.println("测试的key"+issueKey);
//
            http://192.168.11.7:8083/rest/raven/1.0/folderStructure/moveCreatedTestToFolder?entityKey=EP202401&folderId=5&testKey=EP202401-303
            System.out.println("调用移动文件请求");
            MoveFile.moveFile(issueKey,projectKey,folderId);
            //完成之后 进行移动
        }
    }


    @SneakyThrows
    public static Map<String, String> getFormData() {
//        System.out.println("调用获取表单数据");
        String formUrl = "http://192.168.11.7:8083/secure/QuickCreateIssue!default.jspa?decorator=none";
        Header[] headers = new Header[]{new BasicHeader("Cookie", TokenConstant.JSESSIONID),
                new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"),};
        String result = HttpClientUtil.doPostForm(formUrl, new HashMap<>(), headers);
        Map<String, String> map = new HashMap<>();
        if (!result.isEmpty()) {
            JSONObject jsonObject = (JSONObject) JSON.parse(result);
            String formToken = (String) jsonObject.get("formToken");
            String atl_token = (String) jsonObject.get("atl_token");
            map.put("formToken", formToken);
            map.put("atl_token", atl_token);
        }
        System.out.println("创建表单"+map);
        return map;
    }
}
